package com.atguigu.core.controller;


import com.atguigu.core.bean.R;
import com.atguigu.entity.TPay;
import com.atguigu.core.repository.TPayRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 交易支付表 前端控制器
 * </p>
 *
 * @author kashim
 * @since 2024-04-16
 */
@Tag(name = "支付接口")
@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private TPayRepository tPayRepository;

    @Operation(summary = "添加支付")
    @PostMapping("/add")
    public R addPay(@RequestBody TPay pay) {
        TPay tPay = new TPay();
        tPay.setOrderNo(pay.getOrderNo());
        tPay.setUserId(pay.getUserId());
        tPay.setAmount(pay.getAmount());
        return R.ok().data(tPayRepository.save(tPay));
    }

    @Operation(summary = "删除支付")
    @DeleteMapping("/delete/{id}")
    public R deletePay(@PathVariable("id") Integer id) {
        tPayRepository.removeById(id);
        return R.ok().data(tPayRepository.removeById(id));
    }


    @Operation(summary = "更新支付")
    @PutMapping("/update")
    public R updatePay(@RequestBody TPay pay) {
        return R.ok().data(tPayRepository.updateById(pay));
    }


    @Operation(summary = "查询支付")
    @GetMapping("/get/{id}")
    public R getPay(@PathVariable("id") Integer id) {

        return R.ok().data(tPayRepository.getById(id));
    }


    @Value("${server.port}")
    private String port;

    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo) {
        return "atguiguInfo:" + atguiguInfo + "\t " + "port:" + port;

    }


}

